package com.zhuss.product.processor;

import java.util.Date;
import java.util.List;

import com.zhuss.bean.vo.product.line.ListLineSiteVo;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.LineSiteDao;
import com.zhuss.product.processor.cache.SiteCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.site.DeleteSiteBo;
import com.zhuss.bean.vo.product.site.DetailSiteVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.product.db.dao.ext.SiteDao;
import com.zhuss.product.db.entity.Site;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.util.JsonUtil;

/**
 * @description: 站点删除
 * @author: zhushanshuo
 * @create: Wed Dec 30 08:43:41 CST 2020
 **/
@Component("siteDelete")
public class SiteDelete extends AbstractProcessor<Object, DeleteSiteBo> implements Processor<Object, DeleteSiteBo> {

    @Autowired
    private SiteDao siteDao;

    @Autowired
	@Qualifier("siteRead")
	private SiteRead siteRead;

	@Autowired
	private LineSiteDao lineSiteDao;

	@Autowired
	@Qualifier("siteCache")
	private SiteCache siteCache;


    /**
     * 校验
     */
	@Override
    public void check(DeleteSiteBo bo) throws DzcxException {
		Long siteId = bo.getSiteId();
		if (siteId == null) {
			throw new DzcxException(ExceptionCode.站点ID为空);
		}
		//如果站点已经绑定，不能暂停操作
		List<ListLineSiteVo> siteVos = lineSiteDao.listBySiteId(siteId);
		if (siteVos.size() > 0) {
			throw new DzcxException(ExceptionCode.该站点已被线路绑定);
		}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteSiteBo param) throws DzcxException {
    	Long siteId = param.getSiteId();
    	String name = ((DetailSiteVo) siteRead.detail(siteId)).getName();

    	Site record = new Site();
    	record.setSiteId(siteId);
    	record.setDeleted(param.getDeleted());

    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	siteDao.delete(record);

    	param.setName(name);

		siteCache.clear(siteId);
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteSiteBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品.key());
    	record.setAction(LogActionEnum.站点删除.key());
    	record.setTargetId(param.getSiteId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
